package com.qf.lanzhou.blog.api.controller.login;

import com.qf.lanzhou.blog.api.controller.BaseServletPost;
import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.service.RegisterService;
import com.qf.lanzhou.blog.api.service.impl.login.RegisterServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author LEON-HE
 */
@WebServlet("/register.do")
public class RegisterServlet extends BaseServletPost {
    private RegisterService registerService = new RegisterServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("开始注册");
        User registerUser = paramsToEntity(req,User.class);
        try{
            System.out.println("开始注册验证");
            registerService.register(registerUser);
            successResultWrite(null,resp);
            System.out.println("注册成功");
        }catch (RuntimeException e){
            String errorMsg = e.getMessage();
            failedResultWrite(errorMsg,resp);
            System.out.println("注册失败");
        }
    }
}
